package com.example.writting.ui.wordfill

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.example.writting.databinding.FragmentWordFillBinding
import com.google.android.material.tabs.TabLayoutMediator

class WordFillFragment : Fragment() {

    private var _binding: FragmentWordFillBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentWordFillBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setupViewPager()
    }

    private fun setupViewPager() {
        binding.viewPager.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount() = 4

            override fun createFragment(position: Int) = when (position) {
                0 -> FixedCombinationsFragment()
                1 -> WordExplanationsFragment()
                2 -> WordComparisonsFragment()
                3 -> SupplementsFragment()
                else -> throw IllegalStateException("Invalid position $position")
            }
        }

        TabLayoutMediator(binding.tabLayout, binding.viewPager) { tab, position ->
            tab.text = when (position) {
                0 -> "固定组合"
                1 -> "词义解释"
                2 -> "词语对比"
                3 -> "补充"
                else -> throw IllegalStateException("Invalid position $position")
            }
        }.attach()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
} 